/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vienthonga.hibernate.dao;


import com.vienthonga.hibernate.pojo.TUser;
import com.vienthonga.hibernate.utils.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author dangkhoa
 */
public class LoginDAO {    
    public String checkExist(String email, String password) {
        String username = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            String hql = "from TUser where UEmail = :email "
                    +    "and UPassword = :password "
                    +    "and UStatus = 1";

            Query query = session.createQuery(hql);
            query.setString("email", email);
            query.setString("password", password);
            TUser result = (TUser)query.uniqueResult();
            username = result.getUFullname();
            transaction.commit();
        } catch (HibernateException ex) {            
            transaction.rollback();            
        } finally {
            session.close();
        }
        return username;
    }    
}
